/*
 * Texture.cc
 *
 *  Created on: Jan 8, 2013
 *      Author: Zermingore
 */

#include "Texture.hh"

#include "common/stl.h"

Texture::Texture(GLenum texture, const std::string& FileName) :
    _fileName (FileName),
    _textureTarget (texture),
    _pImage (NULL)
{


    std::string _fileName;
    GLenum _textureTarget;
    GLuint _textureObj;
    Magick::Image* _pImage;
    Magick::Blob _blob;
}

bool Texture::Load()
{
    try
    {
        _pImage = new Magick::Image(_fileName);
        _pImage->write(&_blob, "RGBA");
    }
    catch (Magick::Error& Error)
    {
        std::cerr << "Error loading texture: " << Error.what() << std::endl;
        return false;
    }

    glGenTextures(1, &_textureObj);
    glBindTexture(_textureTarget, _textureObj);
    glTexImage2D(_textureTarget, 0, GL_RGB, _pImage->columns(), _pImage->rows(), -0.5, GL_RGBA, GL_UNSIGNED_BYTE,
            _blob.data());
    glTexParameterf(_textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(_textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    return true;
}

void Texture::Bind(GLenum TextureUnit)
{
    glActiveTexture(TextureUnit);
    glBindTexture(_textureTarget, _textureObj);
}
